<?php
/**
 * winbinder package 
 *
 * @package php.ui.winbinder
 */

/**
 * WBGraphics 클래스 
 *
 * @package php.ui.winbinder 
 */ 
class WBGraphics extends PObject { 

	private $_buffer = null;
	private $_com = null;
	private $_x1 = 0;
	private $_y1 = 0;
	private $_x2 = 0;
	private $_y2 = 0;
	private $_width = 0;
	private $_height = 0;
	
	private $_isSetLimit = false;

	const LINE_SOLID			= 0;
	const LINE_DOTTED			= 1;
	const LINE_DASHES_2			= 2;
	const LINE_DASHES_3			= 3;
	const LINE_DASHES_4			= 4;
	const LINE_DASHES_5			= 5;
	const LINE_DASHES_6			= 6;
	const LINE_DASHES_7			= 7;
	const LINE_DASHES_DOTS		= 8;
	const LINE_DASHES_DOUBLE_DOTS = 9;

	const LEFT		= WBC_LEFT;
	const CENTER	=  WBC_CENTER;
	const RIGHT		=  WBC_RIGHT;
	const TOP		=  WBC_TOP;
	const MIDDLE	=  WBC_MIDDLE;
	const BOTTOM	=  WBC_BOTTOM;

	const MULTILINE		=  WBC_MULTILINE;
	const ELLIPSIS		=  WBC_ELLIPSIS;

	public function __construct($buffer = null, $com = null) { 
		$this->_buffer = $buffer;
		$this->_com = $com;
		
		if ($com instanceof WBPanel) { 
			$this->setArea();
		}

	}
	
	public function getBuffer() { 
		return $this->_buffer;
	}
	
	public function setLimitArea($x1, $y1, $x2, $y2) { 
		$this->_x1 = $x1;
		$this->_y1 = $y1;
		$this->_x2 = $x2;
		$this->_y2 = $y2;
		
		$this->_width = $x2 - $x1;
		$this->_height = $y2 - $y1;
	}
	
	public function setArea() { 

		$rect = $this->_com->getRect(true);
		
		$this->setLimitArea($rect['x1'], $rect['y1'], $rect['x2'], $rect['y2']);

		$this->_isSetLimit = true;

	}

	public function setBackgroundColor($color = WHITE) {
		if ($this->_isSetLimit) {
			WBDraw::rect($this->_buffer,$this->_x1, $this->_y1, $this->_width, $this->_height, wbcolor($color), true);  
		} 	
	}
	
	public function drawImageOnBuffer($image, $color = NOCOLOR) { 
		return WBDraw::image($this->_buffer, $image, $this->_x1, $this->_y1, $this->_width, $this->_height, $color);
	}
	
	public function createImageArea() { 
		return new WBImage(array('width' => $this->_width, 'height' => $this->_height));
	}
	
	/**
	 * target에 bitmap 데이타를 그림 
	 *
	 * @param mixed $bitmap 
	 * @param int $x 
	 * @param int $y 
	 * @param int $width
	 * @param int $height 
	 * @param int $transparentcolor 투명도  
	 */
	public function image($bitmap, $x = 0, $y = 0, $width = 0, $height = 0, $transparentcolor = NOCOLOR ) {
		
		if ($this->_isSetLimit) { 
			$image = $this->createImageArea();
			
			WBDraw::image(wbinstance($image), $bitmap, $x, $y, $width, $height, $transparentcolor);
			
			$this->drawImageOnBuffer(wbinstance($image));
			
			$image->destroy();
			
		} else { 
			return WBDraw::image($this->_buffer, $bitmap, $x, $y, $width, $height, $transparentcolor);
		}
	}

	/**
	 * target에 line을 그림 
	 *
	 *
	 */
	public function line($x0, $y0, $x1, $y1, $color = BLACK, $lineWidth = 1, $lineStyle = WBGraphics::LINE_SOLID) {
		if ($this->_isSetLimit) { 
			$image = $this->createImageArea();
			
			WBDraw::line(wbinstance($image), $x0, $y0, $x1, $y1, $color, $lineWidth, $lineStyle);
			
			$this->drawImageOnBuffer(wbinstance($image));
			
			$image->destroy();			
			
		} else { 		
			return WBDraw::line($this->_buffer, $x0, $y0, $x1, $y1, $color, $lineWidth, $lineStyle);
		}		
	}

	/**
	 *
	 * target에 rect(사각형) 을 그림 
	 *
	 */
	public function rect($x, $y, $width, $height, $color = BLACK , $filled = false, $lineWidth = 1, $lineStyle = WBGraphics::LINE_SOLID) {
		if ($this->_isSetLimit) { 
			$image = $this->createImageArea();
			
			WBDraw::rect(wbinstance($image), $x, $y, $width, $height, $color , $filled, $lineWidth, $lineStyle);
			
			$ret = $this->drawImageOnBuffer(wbinstance($image));
			
			$image->destroy();
		
		} else { 			
			return WBDraw::rect($this->_buffer, $x, $y, $width, $height, $color , $filled, $lineWidth, $lineStyle);
		}		
	}

	/**
	 *
	 * target에 ellipse(타원) 을 그림 
	 *
	 */
	public function ellipse($x, $y, $width, $height, $color = BLACK , $filled = false, $lineWidth = 1, $lineStyle = WBGraphics::LINE_SOLID) {
		if ($this->_isSetLimit) { 
			$image = $this->createImageArea();
			
			WBDraw::ellipse(wbinstance($image), $x, $y, $width, $height, $color , $filled, $lineWidth, $lineStyle);
			
			$this->drawImageOnBuffer(wbinstance($image));
			
			$image->destroy();			
			
		} else { 		
			return WBDraw::ellipse($this->_buffer, $x, $y, $width, $height, $color , $filled, $lineWidth, $lineStyle);
		}
	}

	/**
	 * text를 그림 
	 *
	 * <code>
	 * //flags 관련 상수
	 *
	 * WBC_LEFT
	 * WBC_CENTER
	 * WBC_RIGHT
	 * WBC_TOP
	 * WBC_MIDDLE
	 * WBC_BOTTOM
	 * WBC_MULTILINE
	 * WBC_ELLIPSIS
	 * </code>
	 */ 
	public function text($text, $x, $y) {
				
		$arr = func_get_args();
		
		if ($this->_isSetLimit) { 
			$image = $this->createImageArea();
			
			$param = $this->getTextParam(wbinstance($image), $arr);
			
			$ret = call_user_func_array('WBDraw::text', $param);
			
			$this->drawImageOnBuffer(wbinstance($image));
			
			$image->destroy();			
			
		} else { 		

			$param = $this->getTextParam($this->_buffer, $arr);
			
			return call_user_func_array('WBDraw::text', $param);
		}
	}	
	
	public function getTextParam($buffer, $arr) { 
		$param = array($buffer);
		$count = sizeof($arr);
		
		if ($count == 4) {
			$arr[3] = wbinstance($arr[3]);  // font 지정 				 
		} elseif ($count == 5) { 
			$arr[4] = wbinstance($arr[4]);				
		} elseif ($count == 6) { 
			$arr[5] = wbinstance($arr[5]);				
		} elseif ($count == 7) {
			$arr[5] = wbinstance($arr[5]);				 
			$arr[6] = wbconstant($arr[6]);
		}

		$param = array_merge($param, $arr);
		
		return $param;
	}

	/**
	 * 선택한 영역의 pixel의 color 값 얻어오기 
	 *
	 *
	 */
	public function getPixel($x, $y) { 
		if ($this->_isSetLimit) { 
			return wb_get_pixel($this->_buffer, $this->_x1 + $x, $this->_y1 + $y);
		} else { 		
			return wb_get_pixel($this->_buffer, $x, $y);
		}
	}

	/**
	 * 선택한 영역의 pixel의 color 설정 하기 
	 *
	 *
	 */ 
	public function setPixel($x, $y, $color = BLACK) {
		if ($this->_isSetLimit) { 
			return wb_draw_point($this->_buffer, $this->_x1 + $x, $this->_y1 + $y, $color);
		} else { 		
			return wb_draw_point($this->_buffer, $x, $y, $color);
		}
	}

}
?>